package com.flx.dis.registry.zk.controller;

import com.flx.common.utils.result.ResultResponse;
import com.flx.dis.registry.zk.server.ZkServer;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.I0Itec.zkclient.IZkDataListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Fenglixiong
 * @Create 2022/4/19 15:10
 * @Description zk的一些基础api的使用
 **/
@Api
@Slf4j
@RestController
@RequestMapping("/zk")
public class ZkBaseController {

    @Autowired
    private ZkServer zkServer;


    @GetMapping("/connect")
    public ResultResponse<String> connect(@RequestParam("ip")String ip,
                                          @RequestParam(value = "port",defaultValue = "2181")String port){
        try {
            zkServer.connect(ip,port);
            return ResultResponse.success("zk连接成功！");
        }catch (Exception e){
            log.error("zk连接失败，message = {}",e.getCause().toString());
            return ResultResponse.error("zk连接失败，message = {}",e.getMessage());
        }
    }

    @GetMapping("/close")
    public ResultResponse close(){
        try{
            zkServer.close();
            return ResultResponse.success("关闭zk成功！");
        }catch (Exception e){
            log.error("zk关闭失败，message = {}",e.getCause().toString());
            return ResultResponse.error("关闭zk失败，message = {}",e.getMessage());
        }
    }

    @GetMapping("/getData")
    public ResultResponse getData(@RequestParam("path") String path){
        try{
            return ResultResponse.success(zkServer.getData(path));
        }catch (Exception e){
            log.error("获取节点数据失败，message = {}",e.getCause().toString());
            return ResultResponse.error("获取节点数据失败！");
        }
    }

    @GetMapping("/setData")
    public ResultResponse setData(@RequestParam("path") String path,
                                  @RequestParam("data") String data){
        try{
            zkServer.setData(path,data);
            return ResultResponse.success("设置节点数据成功！");
        }catch (Exception e){
            log.error("设置节点数据失败，message = {}",e.getCause().toString());
            return ResultResponse.error("设置节点数据失败！");
        }
    }

    @GetMapping("/createPersistent")
    public ResultResponse createPersistent(@RequestParam("path") String path,
                                     @RequestParam("data") String data){
        try{
            zkServer.createPersistent(path,data);
            return ResultResponse.success("创建持久节点成功！");
        }catch (Exception e){
            log.error("创建持久节点失败，message = {}",e.getCause().toString());
            return ResultResponse.error("创建持久节点失败！");
        }
    }

    @GetMapping("/createEphemeral")
    public ResultResponse createEphemeral(@RequestParam("path") String path,
                                           @RequestParam("data") String data){
        try{
            zkServer.createEphemeral(path,data);
            return ResultResponse.success("创建临时节点成功！");
        }catch (Exception e){
            log.error("创建临时节点失败，message = {}",e.getCause().toString());
            return ResultResponse.error("创建临时节点失败！");
        }
    }

    @GetMapping("/createEphemeralSequential")
    public ResultResponse createEphemeralSequential(@RequestParam("path") String path,
                                          @RequestParam("data") String data){
        try{
            zkServer.createEphemeralSequential(path,data);
            return ResultResponse.success("创建临时序号节点成功！");
        }catch (Exception e){
            log.error("创建临时序号节点失败，message = {}",e.getCause().toString());
            return ResultResponse.error("创建临时序号节点失败！");
        }
    }

    @GetMapping("/getChildren")
    public ResultResponse getChildren(@RequestParam("path") String path){
        try{
            return ResultResponse.success(zkServer.getChildren(path));
        }catch (Exception e){
            log.error("获取子节点失败，message = {}",e.getCause().toString());
            return ResultResponse.error("获取子节点失败！");
        }
    }

    @GetMapping("/subscribeChildChange")
    public ResultResponse subscribeChildChange(@RequestParam("path") String path){
        try{
            zkServer.subscribeChildChange(path, (parentPath, childrenList) -> {
                log.info("ChildChangeEvent path = {},parentPath = {},children = {}",path,parentPath,childrenList);
            });
            return ResultResponse.success("订阅子节点变动成功！");
        }catch (Exception e){
            log.error("订阅子节点变动失败，message = {}",e.getCause().toString());
            return ResultResponse.error("订阅子节点变动失败！");
        }
    }

    @GetMapping("/subscribeDataChange")
    public ResultResponse subscribeDataChange(@RequestParam("path") String path){
        try{
            zkServer.subscribeDataChange(path, new IZkDataListener() {
                @Override
                public void handleDataChange(String path, Object data) throws Exception {
                    log.info("DataChangeEvent path = {},data = {}",path,data);
                }

                @Override
                public void handleDataDeleted(String path) throws Exception {
                    log.info("DataChangeEvent path = {}",path);
                }
            });
            return ResultResponse.success("订阅节点数据变化成功！");
        }catch (Exception e){
            log.error("订阅节点数据变化失败，message = {}",e.getCause().toString());
            return ResultResponse.error("订阅节点数据变化失败！");
        }
    }

    @GetMapping("/unsubscribeAll")
    public ResultResponse unsubscribeAll(){
        try{
            zkServer.unsubscribeAll();
            return ResultResponse.success("解除全部订阅事件成功！");
        }catch (Exception e){
            log.error("解除全部订阅事件失败，message = {}",e.getCause().toString());
            return ResultResponse.error("解除全部订阅事件失败！");
        }
    }

}
